<?php

namespace Common\Model;

use Think\Model;

class TaskModel extends Model
{
    /**
     * 列表
     *
     * 当 chenktask 为 true 时，返回包含 uid 是否已完成任务字段
     *
     * @param mixed $map
     * @param bool  $chenktask
     * @param int   $uid
     * @return array|null|false
     */
    public function get_list($map = '', $chenktask = false, $uid = null)
    {
        $list = $this->where($map)->order('sort DESC, id ASC')->select();
        if ($chenktask && $uid > 0) {
            foreach ($list as $key => $v) {
                $v['chenktask'] = $this->chenk_task($uid, $v['id']);
                if ($v['chenktask']) {
                    $v['chenktask'] = 1;
                } else {
                    $v['chenktask'] = 0;
                }
                $li[] = $v;
            }
            return $li;
        }
        return $list;
    }

    //

    /**
     * 执行每日首次登陆任务
     *
     * @param string $uid
     */
    public function exe_first_login($uid = '')
    {
        $task = $this->get_task_one(13);
        if ($task) {
            $uid = intval($uid);
            $today = strtotime('today');
            $map['uid'] = $uid;
            $map['tid'] = $task['id'];
            $map['addtime'] = array('GT', $today);
            $Log_task = M('Log_task')->where($map)->limit(1)->find();
            if (empty($Log_task)) {
                $this->fulfill_task($task['id'], $uid);
                if ($task['gold'] > 0) {
                    D('User')->edit_user_gold(1, $uid, $task['gold'], '每日首次登陆');
                }
                if ($task['exp'] > 0) {
                    D('User')->edit_user_gold(2, $uid, $task['exp'], '每日首次登陆');
                }
            }
        }
    }

    /**
     * 首次注册
     *
     * @param string $uid
     */
    public function exe_first_reg($uid = '')
    {
        $task = $this->get_task_one(1);
        if ($task) {
            $uid = intval($uid);
            $today = strtotime('today');
            $map['uid'] = $uid;
            $map['tid'] = $task['id'];
            $Log_task = M('Log_task')->where($map)->limit(1)->find();
            if (empty($Log_task)) {
                $this->fulfill_task($task['id'], $uid);
                if ($task['gold'] > 0) {
                    D('User')->edit_user_gold(1, $uid, $task['gold'], '注册赠送');
                }
                if ($task['exp'] > 0) {
                    D('User')->edit_user_gold(2, $uid, $task['exp'], '注册赠送');
                }
            }
        }
    }

    /**
     * 发布问题
     *
     * @param string $uid
     */
    public function exe_add_question($uid = '')
    {
        $task = $this->get_task_one(8);
        if ($task) {
            $ret = $this->fulfill_task($task['id'], $uid);
            if ($ret) {
                if ($task['gold'] > 0) {
                    D('User')->edit_user_gold(1, $uid, $task['gold'], '发表内容获得奖励');
                }
                if ($task['exp'] > 0) {
                    D('User')->edit_user_gold(2, $uid, $task['exp'], '发表内容获得奖励');
                }
            }
        }
    }

    /**
     * 发布文章
     *
     * @param string $uid
     */
    public function exe_add_article($uid = '')
    {
        $task = $this->get_task_one(10);
        if ($task) {
            $ret = $this->fulfill_task($task['id'], $uid);
            if ($ret) {
                if ($task['gold'] > 0) {
                    D('User')->edit_user_gold(1, $uid, $task['gold'], '分享文章获得奖励');
                }
                if ($task['exp'] > 0) {
                    D('User')->edit_user_gold(2, $uid, $task['exp'], '分享文章获得奖励');
                }
            }
        }
    }

    /**
     * 发表回复
     *
     * @param string $uid
     */
    public function exe_reply_question($uid = '')
    {
        $task = $this->get_task_one(9);
        if ($task) {
            $ret = $this->fulfill_task($task['id'], $uid);
            if ($ret) {
                if ($task['gold'] > 0) {
                    D('User')->edit_user_gold(1, $uid, $task['gold'], '发表回复获得奖励');
                }
                if ($task['exp'] > 0) {
                    D('User')->edit_user_gold(2, $uid, $task['exp'], '发表回复获得奖励');
                }
            }
        }
    }

    /**
     * 采纳答案，提问人获得奖励
     *
     * @param string $uid
     */
    public function exe_adoption_answer($uid = '')
    {
        $task = $this->get_task_one(12);
        if ($task) {
            $ret = $this->fulfill_task($task['id'], $uid);
            if ($ret) {
                if ($task['gold'] > 0) {
                    D('User')->edit_user_gold(1, $uid, $task['gold'], '采纳答案系统奖励');
                }
                if ($task['exp'] > 0) {
                    D('User')->edit_user_gold(2, $uid, $task['exp'], '采纳答案系统奖励');
                }
            }
        }
    }

    /**
     * 采纳答案，答题人获得奖励
     *
     * @param string $uid
     */
    public function exe_reply_adoption($uid = '')
    {
        $task = $this->get_task_one(11);
        if ($task) {
            $ret = $this->fulfill_task($task['id'], $uid);
            if ($ret) {
                if ($task['gold'] > 0) {
                    D('User')->edit_user_gold(1, $uid, $task['gold'], '答案被采纳系统奖励');
                }
                if ($task['exp'] > 0) {
                    D('User')->edit_user_gold(2, $uid, $task['exp'], '答案被采纳系统奖励');
                }
            }
        }
    }

    /**
     * 完善基本资料
     *
     * @param string $uid
     */
    public function exe_user_specialty($uid = '')
    {
        $task = $this->get_task_one(2);
        $chenk = $this->chenk_task($uid, 2);
        if ($task && empty($chenk)) {
            $this->fulfill_task($task['id'], $uid);
            if ($task['gold'] > 0) {
                D('User')->edit_user_gold(1, $uid, $task['gold'], '完善基本资料');
            }
            if ($task['exp'] > 0) {
                D('User')->edit_user_gold(2, $uid, $task['exp'], '完善基本资料');
            }
        }
    }

    /**
     * 上传会员头像
     *
     * @param string $uid
     */
    public function exe_user_avatar($uid = '')
    {
        $task = $this->get_task_one(3);
        $chenk = $this->chenk_task($uid, 3);
        if ($task && empty($chenk)) {
            $this->fulfill_task($task['id'], $uid);
            if ($task['gold'] > 0) {
                D('User')->edit_user_gold(1, $uid, $task['gold'], '上传头像');
            }
            if ($task['exp'] > 0) {
                D('User')->edit_user_gold(2, $uid, $task['exp'], '上传头像');
            }
        }
    }

    /**
     * 执行违法举报任务
     * @param string $uid
     */
    public function exe_content_report($uid = '')
    {
        $task = $this->get_task_one(7);
        if ($task) {
            $ret = $this->fulfill_task($task['id'], $uid);
            if ($ret) {
                if ($task['gold'] > 0) {
                    D('User')->edit_user_gold(1, $uid, $task['gold'], '违法举报系统奖励');
                }
                if ($task['exp'] > 0) {
                    D('User')->edit_user_gold(2, $uid, $task['exp'], '违法举报系统奖励');
                }
            }
        }
    }

    /**
     * 增加完成任务日志
     *
     * @param string $taskid
     * @param string $uid
     * @return bool
     */
    public function fulfill_task($taskid = '', $uid = '')
    {
        $task = M('Task')->find($taskid);
        if ($task['type'] == '2') {
            $map['tid'] = $taskid;
            $map['uid'] = $uid;
            $today = strtotime('today');
            $map['addtime'] = array('GT', $today);
            $count = M("LogTask")->where($map)->count();
            if ($count >= $task['limit']) return false;
        }

        $data['tid'] = intval($taskid);
        $data['uid'] = intval($uid);
        $data['addtime'] = time();
        $data['entrance'] = C('YTATA_ENTRANCE');
        M("LogTask")->add($data);
        return true;
    }

    /**
     * 获取一个有效任务
     *
     * @param int $id 任务ID
     * @param int $display
     * @return mixed
     */
    public function get_task_one($id = null, $display = 1)
    {
        $map['id'] = intval($id);
        $map['is_display'] = $display;
        $task = $this->where($map)->find();
        return $task;
    }

    /**
     * 检测任务是否完成
     *
     * @param int $uid
     * @param int $tid
     * @return array|null|bool
     */
    public function chenk_task($uid = null, $tid = null)
    {
        $map['uid'] = intval($uid);
        $map['tid'] = intval($tid);
        $task = M("LogTask")->where($map)->find();
        return $task;
    }

}